//
//  SocketEnginePollable.h
//  socket.io-OC
//
//  Created by leeco on 16/8/9.
//  Copyright © 2016年 leeco. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SocketEngineSpec.h"
#import "SocketEnginePacketType.h"
#import "SocketIOClientSpec.h"
// SocketEnginePollableProtocol 代理
@protocol SocketEnginePollableProtocol <NSObject,SocketIOClientSpecProtocol>

@property (nonatomic, assign) BOOL invalidated;
/// Holds strings waiting to be sent over polling.
/// You shouldn't need to mess with this.
@property (nonatomic, strong) NSMutableArray<NSString *> *postWait;
@property (nonatomic, strong) NSURLSession *session;
/// Because socket.io doesn't let you send two polling request at the same time
/// we have to keep track if there's an outstanding poll
@property (nonatomic, assign) BOOL waitingForPoll;
/// Because socket.io doesn't let you send two post request at the same time
/// we have to keep track if there's an outstanding post
@property (nonatomic, assign) BOOL waitingForPost;

//协议定义方法
- (void)doPoll;
- (void)sendPollMessage:(NSString *)message withType:(SocketEnginePacketType)type withData:(NSArray<NSData *> *)datas;
- (void)stopPolling;

@end

@interface SocketEnginePollable : SocketEngineSpec <SocketEnginePollableProtocol>

//@property (nonatomic, assign) BOOL invalidated;
//@property (nonatomic, copy) NSMutableArray<NSString *> *postWait;
//@property (nonatomic, strong) NSURLSession *session;
//@property (nonatomic, assign) BOOL waitingForPoll;
//@property (nonatomic, assign) BOOL waitingForPost;


//协议扩展实现方法
- (void)addHeaders:(NSMutableURLRequest *)req;
- (NSURLRequest *) createRequestForPostWithPostWait;
- (void)doPoll;
- (void)doRequest:(NSURLRequest *)req withCallback:(void (^)(NSData *, NSURLResponse *, NSError *))callback;
- (void)doLongPoll:(NSURLRequest *)req;
- (void)flushWaitingForPost;
- (void)parsePollingMessage:(NSString *)str;
/// Send polling message.
/// Only call on emitQueue
- (void)sendPollMessage:(NSString *)message withType:(SocketEnginePacketType)type withData:(NSArray<NSData *> *)datas;
- (void)stopPolling;

@end
